package com.mx.zb.mng.ctrl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mx.zb.mng.bo.Agent;
import com.mx.zb.mng.bo.BizIndustry;
import com.mx.zb.mng.bo.Template;
import com.mx.zb.mng.bo.Tenant;
import com.mx.zb.mng.dao.AgentMapper;
import com.mx.zb.mng.dao.BizIndustryMapper;
import com.mx.zb.mng.dao.TemplateMapper;
import com.mx.zb.mng.dao.TenantMapper;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.db2.Page;
import com.mx.zb.mng.utils.db2.PageHelper;
import com.mx.zb.mng.utils.ex.PageAnnotation;

@Controller
@PageAnnotation
@RequestMapping("/p/biz")
public class TenantCtrl {

	@Autowired
	TenantMapper tenantMapper;
	@Autowired
	AgentMapper  agentMapper;
	@Autowired
	BizIndustryMapper bizIndustryMapper;
	
	@Autowired
	TemplateMapper templateMapper;

	@RequestMapping(value = { "/tenant" })
	public String totenant(Model model) {
		PageHelper.startPage();
		Tenant o = new Tenant();
		o.setAgentId(ContextThread.get().getAgentId());
		Page<Tenant> pages = tenantMapper.queryPage(o);
		model.addAttribute("agentList", pages);
		List<BizIndustry> tmpList = bizIndustryMapper.selectTemplates();
		model.addAttribute("tpList", tmpList);
		return "TenantLists";
	}

	@RequestMapping(value = { "/tenant/totenant" })
	public String toaddtenant(Model model) {
		// Agent
		// parentAgent=agentMapper.selectByPrimaryKey(ContextThread.get().getAgentId());//当前创建人所在的代理商
		// String [] templates=parentAgent.getTemplateId().split(",");
		/*List<Template> templist = templateMapper.findAll();
		model.addAttribute("templates", templist);*/
		BizIndustry o=new BizIndustry();
		o.setTemplate("零售电商");;
		List<BizIndustry> listIndustry=bizIndustryMapper.queryList(o);
		model.addAttribute("listIndustry", listIndustry);
		return "addTenant";
	}

	@RequestMapping(value = { "/tenant/show" })
	public String showtenant(@RequestParam String id, Model model) {
		Tenant o = tenantMapper.selectByPrimaryKey(id);
		model.addAttribute("tenant", o);
		// Operator op = operatorMapper.selectByPrimaryKey(o.getCreateOper());
		Agent a = agentMapper.selectByPrimaryKey(o.getAgentId());
		model.addAttribute("OperatorName", a.getName());
		/*List<Template> templist = templateMapper.findAll();
		model.addAttribute("templates", templist);*/
		BizIndustry b=new BizIndustry();
		if(o!=null) {
			b.setTemplate(o.getTemplateId());
		}
		List<BizIndustry> listIndustry=bizIndustryMapper.queryList(b);
		model.addAttribute("listIndustry", listIndustry);
		return "TenantEdit";
	}

}
